package com.mobilepay.moneypig.model.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseDialog;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.response.mine.SceneData;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;
import com.mobilepay.moneypig.utils.acache.ACache;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xlm on 2017/8/7 0007
 */

public class SaiXuanDialog extends BaseDialog {

    public SaiXuanDialog(Context context) {
        super(context);
    }

    public SaiXuanDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.dialog_saixuan);
    }

    @Override
    protected void initViews() {
        mRecycleSX = (RecyclerView) findViewById(R.id.sx_recycle);
        cancle = (TextView) findViewById(R.id.sx_cancle);
        initWindow(1.0f, 0, Gravity.BOTTOM);
        setAnimate(R.style.animatedialog);
    }

    @Override
    protected void onSetListener() {
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        setSceneAdapter();
        getTitleList();
    }

    private List<SceneData.DataBean> list;
    private SXAdapter sxAdapter;
    private RecyclerView mRecycleSX;
    private TextView cancle;

    private void setSceneAdapter() {
        list = new ArrayList<>();
        sxAdapter = new SXAdapter(getActivity());
        sxAdapter.setData(list);
        mRecycleSX.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        mRecycleSX.setAdapter(sxAdapter);
    }

    private void getTitleList() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.getScene);
        param.add("uid", String.valueOf(App.getApp().getUserInfo().getData().getUid()));
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                if (CommonUtil.isForeground(getActivity())) {
                    showToastShort(getActivity().getString(R.string.net_state));
                    String jsonString = ACache.get(App.getApp()).getAsString(NetConfig.getScene);
                    if (!CommonUtil.isEmpty(jsonString)) {
                        setSxData(jsonString);
                    }
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (responseResult.getStatus() == 1) {
                        ACache.get(App.getApp()).put(NetConfig.getScene, string);
                        setSxData(string);
                    } else {
                        showToastShort(responseResult.getInfo());
                    }
                }
            }
        });
    }

    private void setSxData(String string) {
        SceneData sceneData = GsonUtil.GsonToBean(string, SceneData.class);
        List<SceneData.DataBean> beanList = sceneData.getData();
        if (beanList.size() != 0) {
            list.clear();
            list.addAll(beanList);
            sxAdapter.setData(list);
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    sxAdapter.notifyDataSetChanged();
                }
            });
        }
    }

    private class SXAdapter extends RecyclerView.Adapter<SXHolder> {
        private Context context;
        private LayoutInflater inflater;
        private List<SceneData.DataBean> list;
        private List<SceneData.DataBean> newlist;
        private SceneData.DataBean dataBean;

        public SXAdapter(Context context) {
            this.context = context;
            inflater = LayoutInflater.from(context);
            dataBean = new SceneData.DataBean();
            dataBean.setName("全部");
            dataBean.setId(0);
            dataBean.setTable_name("");
            newlist = new ArrayList<>();
        }

        private void setData(List<SceneData.DataBean> list) {
            this.list = list;
            if (list.size() != 0) {
                for (int i = 0; i < list.size() + 1; i++) {
                    if (i == 0) {
                        newlist.add(dataBean);
                    } else {
                        newlist.add(list.get(i - 1));
                    }
                }
            }
        }

//        @Override
//        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
//            GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
//            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//                @Override
//                public int getSpanSize(int position) {
//                    if (position == 0) {
//                        return 3;
//                    }
//                    return 3;
//                }
//            });
//        }

        @Override
        public SXHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new SXHolder(inflater.inflate(R.layout.item_sx, parent, false));
        }

        @Override
        public void onBindViewHolder(SXHolder holder, int position) {
            final SceneData.DataBean dataBean = newlist.get(position);
            if (dataBean != null) {
                holder.mTextLable.setText(dataBean.getName());
                holder.mTextLable.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.clickLable(String.valueOf(dataBean.getId()), dataBean.getName());
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return newlist.size();
        }

    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    private Listener listener;

    public static class Listener {
        public void clickLable(String id, String title) {

        }
    }

    private static class SXHolder extends RecyclerView.ViewHolder {
        private TextView mTextLable;

        private SXHolder(View itemView) {
            super(itemView);
            mTextLable = (TextView) itemView.findViewById(R.id.sx_lable);
        }
    }
}
